const _ = require('underscore')

let that = null

Page({
  data: {
    inputValue: '',
    list: []
  },

  onReady() {
    that = this
  },

  _fetchData() {
    wx.request({
      url: 'http://localhost:9000/data?message_like=' + this.data.inputValue + '&contact_like=' + this.data.inputValue,
      success: (res) => {
        console.log(res)
        this.setData({
          list: res.data
        })
      }
    })
  },

  handleSearchTap() {
    this._fetchData()
  },

  handleInputTap: _.debounce((e) => {
    // console.log(that)
    const currentPage = (getCurrentPages().slice(-1))[0]
    
    currentPage.setData({
      inputValue: e.detail.value
    })

    currentPage._fetchData()
  }, 300)

  // 不能使用call
  // handleInputTap: _.debounce.call(this, (e) => {
  //   console.log(this)
  //   const currentPage = (getCurrentPages().slice(-1))[0]
    
  //   currentPage.setData({
  //     inputValue: e.detail.value
  //   })

  //   currentPage._fetchData()
  // }, 300)

  // 一层柯里化也不能解决问题
  // handleInputTap() {
  //   const that = this
  //   return _.debounce((e) => {
  //     console.log(this)
      
  //     that.setData({
  //       inputValue: e.detail.value
  //     })
  
  //     that._fetchData()
  //   }, 300)
  // }
})